#!/bin/bash

function link_po {
	# $1 = language directory (eg: km, zn, th, etc.)
	# $2 = name of file to look for (mbds.po | mbds_dynamic.po)
	# $3 = pootle project of file to link (scorecardui | scorecarddb) 
	
	f="/opt/mbds-scorecards/po/$1/$2"
	
	if [ -e $f ]; then
		dd="/var/lib/pootle/po/$3/$1"
		if [ -d $dd ]; then
			echo "Directory $dd exists. Will directly link po file."
		else
			echo "Creating directory $dd."
			mkdir $dd
		fi

		df="$dd/$3.po"
		echo "Checking existence of $df..."
		if [ -L $df ]; then
			echo "File $dd/$3.po is already linked. Going on to next file."
		else
			echo "Linking file $df"
			ln -s $f $df
		fi
	fi
}

for d in $(ls -d -1 /opt/mbds-scorecards/po/*/ | xargs -n1 basename); do
	link_po $d "mbds.po" "scorecardui"
	link_po $d "mbds_dynamic.po" "scorecarddb"
done

chown -R www-data:www-data ../po
chown -R www-data:www-data /var/lib/pootle/po
